import { MiniGameActionObject } from "../../MiniGameActionObject";
import { HookController } from "../ObjController/HookController";
import { HookState } from "../defin/HookState";
import { ScaleBitmap } from "../../../../../../com/mui/display/ScaleBitmap";
import { GlobalClass } from "../../../../../../com/mui/core/GlobalClass";
import { FishingGlobal } from "../FishingGlobal";
import { Fish } from "./Fish";
import { Obstacle } from "./Obstacle";
import { Dispatcher } from "../../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../../events/DataEvent";
import { EventName } from "../../../../mvc/EventName";
import { FishingScene } from "../FishingScene";
import { TweenLite } from "../../../../../../com/greensock/TweenLite";
//class Hook
    
    export  class Hook extends MiniGameActionObject
    {
        constructor(arg1: HookController)
        {
            super(arg1);
            this._hookState = HookState.swing;
            this.draw();
            return;
        }

        /* internal  */draw(): void
        {
            this._rope = new ScaleBitmap();
            this._rope = GlobalClass.getScaleBitmap("FishingLine", new Object /* flash.geom.Rectangle */(0, 0, 3, 143));
            this._rope.height = this.BaseLength;
            this.addChild(this._rope);
            this._hook = GlobalClass.getBitmap("Hook");
            this._hook.x = -14;
            this._hook.y = this.BaseLength;
            this.addChild(this._hook);
            this._aryBaseImpactPoints = [new Object /* flash.geom.Point */(0, 15), new Object /* flash.geom.Point */(17, 20), new Object /* flash.geom.Point */(32, 15)];
            return;
        }

        public /* override */ Do(): void
        {
            var loc2=null;
            var loc3=null;
            var loc4=null;
            var loc5=null;
            var loc6=null;
            var loc7=null;
            (controller as HookController).autoRun();
            var loc1=false;
            if (this._hookState != HookState.swing) 
            {
                this._length = this._length + this._speed;
                if (this._length <= 0) 
                {
                    this._length = 0;
                }
                if (this._hookState == HookState.down) 
                {
                    var loc8=0;
                    var loc9=FishingGlobal.scene.allObject;
                    for(loc3 of loc9) 
                    {
                        loc4 = new Array<any>();
                        loc5 = this.getHookMatrix();
                        var loc10=0;
                        var loc11=this._aryBaseImpactPoints;
                        for(loc2 of loc11) 
                        {
                            (loc6 = new Object /* flash.geom.Point */()).x = loc2.x * loc5.a + loc2.y * loc5.c + loc5.tx;
                            loc6.y = loc2.x * loc5.b + loc2.y * loc5.d + loc5.ty;
                            loc6 = this.localToGlobal(loc6);
                            loc4.push(loc6);
                        }
                        if (typeof loc3 === "fish") 
                        {
                            loc10 = 0;
                            loc11 = loc4;
                            for(loc2 of loc11) 
                            {
                                if (!loc3.hitTestPoint(loc2.x, loc2.y)) 
                                {
                                    continue;
                                }
                                loc1 = true;
                                this._isHitFish = true;
                                this.hookFish(loc3 as Fish);
                                break;
                            }
                        }
                        if (typeof !(loc3 === "obstacle")) 
                        {
                            continue;
                        }
                        loc10 = 0;
                        loc11 = loc4;
                        for(loc2 of loc11) 
                        {
                            if (!loc3.hitTestPoint(loc2.x, loc2.y)) 
                            {
                                continue;
                            }
                            loc1 = true;
                            (loc3 as Obstacle).shake(0.2);
                            break;
                        }
                    }
                }
                if (this._hookState == HookState.up) 
                {
                    if (this._length == 0) 
                    {
                        this._hookState = HookState.swing;
                        if (this._hookFish) 
                        {
                            this.tweenFish(this._hookFish);
                        }
                        this._hookFish = null;
                        Dispatcher.dispatchEvent(new DataEvent(EventName.FishMiniGameFishOne, this._isHitFish));
                        this._isHitFish = false;
                    }
                }
                if (this._hookFish) 
                {
                    loc7 = this.getHookMatrix();
                    this._hookFish.x = this.x + 15 + loc7.tx;
                    this._hookFish.y = loc7.ty + this._hookFish.height / 2;
                }
            }
            this._rope.transform.matrix = this.getLineMatrix();
            this._hook.transform.matrix = this.getHookMatrix();
            this._hook.smoothing = true;
            this._rope.smoothing = true;
            if (this._length > FishingScene.AREAHEIGHT || loc1) 
            {
                this._hookState = HookState.up;
            }
            return;
        }

        /* internal  */tweenFish(arg1: Fish): void
        {
            var fish: Fish;

            var loc1;
            fish = arg1;
            TweenLite.to(fish, 0.5, {"x": 100, "y": 30, "onComplete":(): void
            {
                FishingGlobal.scene.delObject(fish);
                return;
            }})
            return;
        }

        /* internal  */getHookMatrix(): Object /* flash.geom.Matrix */
        {
            var loc1=new Object /* flash.geom.Matrix */();
            loc1.tx = loc1.tx - 16;
            loc1.ty = loc1.ty + (this._length + this.BaseLength);
            if (this.angle % 360 != 0) 
            {
                loc1.rotate(this.angle * Math.PI / 180);
            }
            loc1.tx = loc1.tx + 2;
            return loc1;
        }

        /* internal  */getLineMatrix(): Object /* flash.geom.Matrix */
        {
            var loc1=new Object /* flash.geom.Matrix */();
            loc1.tx = loc1.tx - 1.5;
            loc1.scale(1, (this._length + this.BaseLength) / this.BaseLength);
            if (this.angle % 360 != 0) 
            {
                loc1.rotate(this.angle * Math.PI / 180);
            }
            loc1.tx = loc1.tx + 1.5;
            return loc1;
        }

        public /* override */ dispose(arg1: Boolean=true): void
        {
            super.dispose();
            return;
        }

        public hookFish(arg1: Fish): void
        {
            arg1.dead();
            this._hookFish = arg1;
            return;
        }

        public get angle(): number
        {
            return this._angle;
        }

        public set angle(arg1: number)
        {
            this._angle = arg1;
            return;
        }

        public get speed(): number
        {
            return this._speed;
        }

        public set speed(arg1: number)
        {
            this._speed = arg1;
            return;
        }

        public get hookState(): string
        {
            return this._hookState;
        }

        public set hookState(arg1: string)
        {
            this._hookState = arg1;
            return;
        }

        public get length(): number
        {
            return this._length;
        }

        /* internal */ /* const */BaseLength: number=20;

        protected /* var */_rope: ScaleBitmap;

        protected /* var */_hook: Object /* flash.display.Bitmap */;

        protected /* var */_hookState: string;

        protected /* var */_angle: number=0;

        protected /* var */_speed: number=0;

        protected /* var */_length: number=0;

        protected /* var */_isHitFish: Boolean=false;

        protected /* var */_hookFish: Fish;

        protected /* var */_aryBaseImpactPoints: Array<any>;
    }
